<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Node extends BaseController {
	function Node(){
		parent::BaseController();
		
		$this->load->model("Node_Model", "Nodes");

		$this->load->helper('date');
		$this->template->addData("date_format", "%d/%m/%Y");
		$this->template->setPage("node/index");
	}

	function index($nodeIdentifier = NULL){

		if($nodeIdentifier != NULL && $nodeIdentifier != ""){
			$node = NULL;
			
			$node_params = array();
			if(is_numeric($nodeIdentifier)){
				$node_params["id"] = $nodeIdentifier;
			} else{
				$node_params["url_name"] = $nodeIdentifier;
			}
			
			// Se tendria que tratar por permisos de redactar contenidos o similar...
			if($this->redux_auth->logged_in() && $this->redux_auth->profile()->group == "admin"){
				$node_params['status'] = "all";
			}
			
			$cache_time = intval($this->settings->item("_site_cache_nodes_time"));
			$node = $this->mp_cache->model("Nodes", "get", array($node_params), $cache_time);
			
			if($node != NULL){
				$this->Nodes->updateCounters($node->id);
				
				//TODO Hits del node
				
				$this->template->addData("node", $node);
				$this->template->addData("data", unserialize($node->data));
				$this->template->addData("metas", $this->mp_cache->model("Nodes", "getMeta", array($node->id), $cache_time));
				$this->template->addData("tags", $this->mp_cache->model("Nodes", "getTags", array($node->id), $cache_time));
				$this->template->addData("categories", $this->mp_cache->model("Nodes", "getCategories", array($node->id, "juegos"), $cache_time));
				
				$this->template->render();
			} else{
				//redirect("error/error_404");
				//show_404();
			}
		} else{
			//redirect("error/error_404");
			//show_404();
		}
	}
	
	function _remap($nodeIdentifier = NULL){
		$this->index($nodeIdentifier);
	}
}
?>
